<?

//Data loading
if ((GetParam("fromMonth","")=="AUDIT") || (GetParam("toMonth","")=="AUDIT"))
    $title='LAPORAN PERUBAHAN EKUITAS (AUDIT)';
else
    $title='LAPORAN PERUBAHAN EKUITAS';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport('P',"mm","Letter");
$pdf->AddPage();
//$Result = GridList ();
$pdf->GridTable();
$pdf->Output();


function Get_PaidCapital() {
  global $DBConnection;
  $SQL = "SELECT akumulasi FROM tblcoa_totaltmp WHERE skada='5001'";
  return $DBConnection->dbc->get_var($SQL);
}

function Get_PaidLaba() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='5004' OR skada='5005'";
  return $DBConnection->dbc->get_var($SQL);
}

function Get_SelisihAktiva() {
  global $DBConnection;
  $SQL = "SELECT akumulasi FROM tblcoa_totaltmp WHERE skada='5002'";
  return $DBConnection->dbc->get_var($SQL);
}

function Get_RelatedParties() {
  global $DBConnection;
  $SQL = "SELECT akumulasi FROM tblcoa_totaltmp WHERE skada='5003'";
  return $DBConnection->dbc->get_var($SQL);
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
//  $strdate = date()
  return $arraymonth[$month].", $year";
}

function CreateDummyCOA ($PYear,$PMonth) {
    global $DBConnection;

    //$TEMP = "TEMPORARY";

    $Year   = $PYear;
    $LastYear = $Year-1;
    $Month  = $PMonth;
    if ($Month=="AUDIT") $Month=13;
    if ($Month<10) $Month = "0".$Month;
    if (($Month-1)<10) $LastMonth = "0".($Month-1);

    if ($Month<2) $LastMonth = "awal as lastmonth";
    else {$LastMonth = "aku".$LastMonth." as lastmonth";}

    $Mutasi = "(mutd".$Month."-"."mutk".$Month.") as mutasi";
    $CurrentMonth = "aku".$Month." as cmonth";

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp_last`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `lastakumulasi` decimal( 16,4) NOT NULL,
                `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp_last` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
                `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp_last
        SELECT skada, $CurrentMonth, ktrasl
        FROM tblcoa WHERE tipe=1 AND ledger='$LastYear'
    ";

    if (GetParam("office",""))  $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp
        SELECT tblcoa.skada, tblcoa.pathprn, tblcoa.namacc, tblcoa.parent, tblcoa.printing, tblcoa.jumlah, tblcoa.grup, $CurrentMonth, tblcoatmp_last.akumulasi, tblcoa.ktrasl
        FROM tblcoa LEFT JOIN tblcoatmp_last ON tblcoatmp_last.skada=tblcoa.skada AND tblcoatmp_last.ktrasl=tblcoa.ktrasl
        WHERE tipe=1 AND ledger='$Year'
    ";

    if (GetParam("office",""))      $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "DROP TABLE IF EXISTS `tblcoa_totaltmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoa_totaltmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `lastakumulasi` decimal( 16,4) NOT NULL,
                `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL  = "
        INSERT INTO tblcoa_totaltmp
        SELECT tblcoatmp.skada, tblcoatmp.pathprn, tblcoatmp.namacc, tblcoatmp.parent, tblcoatmp.printing,
                tblcoatmp.jumlah, tblcoatmp.grup, sum(aku$Month), lastakumulasi, tblcoa.ktrasl
        FROM tblcoatmp, tblcoa
        WHERE tblcoa.pathprn like concat(tblcoatmp.pathprn,'%')
                AND tblcoatmp.ktrasl=tblcoa.ktrasl

    ";

        if (GetParam("office","")) $SQL .= " AND tblcoa.ktrasl='".GetParam("office","")."' ";

    $SQL .= "
        GROUP BY tblcoatmp.skada
                ";
        //print $SQL."<p>\n";die();
    $DBConnection->dbc->query($SQL);
}

class PDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('Arial','','arialn.php');
            $DEFINE_FONT=1;
        }


        $this->SetFont('Arial','',10);
        $this->Cell(200,5,$perush,0,1,'C',0);
        $this->Cell(200,5,$title,0,1,'C',0);

        if (GetParam("m","")=="AUDIT")
        $this->Cell(200,5,"PERIOD: ".strtoupper(date("d F, Y", mktime(0, 0, 0, 13, 0, GetParam("fromYear","")))),0,1,'C',0);
        else
        $this->Cell(200,5,"PERIOD: ".strtoupper(date("d F, Y", mktime(0, 0, 0, GetParam("fromMonth","")+1, 0, GetParam("fromYear",""))))." - ".strtoupper(date("d F, Y", mktime(0, 0, 0, GetParam("toMonth","")+1, 0, GetParam("toYear","")))),0,1,'C',0);
        $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);
        $this->Line(10,30,210,30);
        $this->Ln();
        $this->Ln();
        //Header

        //Column widths
        $w=array(50,30,30,30,30,30);
        $this->SetFont('Arial','',8);
        $this->Cell($w[0],10,"","LRT",0,'C',0);
        $this->Cell($w[1],5,"MODAL SAHAM","LRTB",0,'C',0);
        $this->Cell($w[2],5,"SELISIH AKTIVA TETAP","LRTB",0,'C',0);
        $this->Cell($w[3],5,"RELATED PARTY TRANS.","TRB",0,'C',0);
        $this->Cell($w[4],5,"SALDO LABA","TRB",0,'C',0);
        $this->Cell($w[5],5,"JUMLAH EKUITAS","LRTB",0,'C',0);
        $this->Ln();


    }

    function GridTable() {
        //Column widths
        $w=array(50,30,30,30,30,30);

        //Data Saldo From Period
        CreateDummyCOA (GetParam("fromYear",""),GetParam("fromMonth",""));
        $FromPaidLaba = Get_PaidLaba();
        if ($FromPaidLaba<0) $FromPaidLaba = $FromPaidLaba * -1;
        $FromPaidInCapital = Get_PaidCapital();
        if ($FromPaidInCapital<0) $FromPaidInCapital = $FromPaidInCapital * -1;
        $FromSelisihAktiva = Get_SelisihAktiva();
        if ($FromSelisihAktiva<0) $FromSelisihAktiva = $FromSelisihAktiva * -1;
        $FromRelatedParties = Get_RelatedParties();
        if ($FromRelatedParties<0) $FromRelatedParties = $FromRelatedParties * -1;

        $FromTotalEquity = $FromPaidLaba + $FromPaidInCapital + $FromSelisihAktiva + $FromRelatedParties;

        $this->Cell($w[0],7,"Saldo, ".date("d F, Y", mktime(0, 0, 0, GetParam("fromMonth","")+1, 0, GetParam("fromYear",""))),'LR');
        $this->Cell($w[1],7,FormatNumber($FromPaidInCapital),'LR',0,'R');
        $this->Cell($w[2],7,FormatNumber($FromSelisihAktiva),'LR',0,'R');
        $this->Cell($w[3],7,FormatNumber($FromRelatedParties),'LR',0,'R');
        $this->Cell($w[4],7,FormatNumber($FromPaidLaba),'LR',0,'R');
        $this->Cell($w[5],7,FormatNumber($FromTotalEquity),'LR',0,'R');
        $this->Ln();

        CreateDummyCOA (GetParam("toYear",""),GetParam("toMonth",""));
        $ToPaidLaba = Get_PaidLaba();
        if ($ToPaidLaba<0) $ToPaidLaba = $ToPaidLaba * -1;
        $ToPaidInCapital = Get_PaidCapital();
        if ($ToPaidInCapital<0) $ToPaidInCapital = $ToPaidInCapital * -1;
        $ToSelisihAktiva = Get_SelisihAktiva();
        if ($ToSelisihAktiva<0) $ToSelisihAktiva = $ToSelisihAktiva * -1;
        $ToRelatedParties = Get_RelatedParties();
        if ($ToRelatedParties<0) $ToRelatedParties = $ToRelatedParties * -1;

        $ToTotalEquity = $ToPaidLaba + $ToPaidInCapital + $ToSelisihAktiva + $ToRelatedParties;
        $ToPaidLabaBalance = $ToPaidLaba - $FromPaidLaba;
        if ($ToPaidLabaBalance<0) $ToPaidLabaBalance = $ToPaidLabaBalance * -1;

        //Data Rugi/Laba bersih
        $this->Cell($w[0],7,"Rugi/Laba bersih ",'LR');
        $this->Cell($w[1],7,"-",'LR',0,'R');
        $this->Cell($w[2],7,"-",'LR',0,'R');
        $this->Cell($w[3],7,"-",'LR',0,'R');
        $this->Cell($w[4],7,FormatNumber($ToPaidLabaBalance),'LR',0,'R');
        $this->Cell($w[5],7,FormatNumber($ToPaidLabaBalance),'LR',0,'R');
        $this->Ln();

        //Penambahan modal
        $AddPaidInCapital = $ToPaidInCapital-$FromPaidInCapital;
        $AddSelisihAktiva = $ToSelisihAktiva-$FromSelisihAktiva;
        $AddPaidLaba      = $ToPaidLaba-$FromPaidLaba;
        $AddTotalEquity   = $ToTotalEquity-$FromTotalEquity;

        $this->Cell($w[0],7,"Penambahan modal disetor ",'LR');
        $this->Cell($w[1],7,"-",'LR',0,'R');
        $this->Cell($w[2],7,"-",'LR',0,'R');
        $this->Cell($w[3],7,"-",'LR',0,'R');
        $this->Cell($w[4],7,"-",'LR',0,'R');
        $this->Cell($w[5],7,FormatNumber($AddPaidInCapital),'LR',0,'R');
        $this->Ln();

        //Data Saldo To Period
        $this->Cell($w[0],7,"Saldo, ".date("d F, Y", mktime(0, 0, 0, GetParam("toMonth","")+1, 0, GetParam("toYear",""))),'LR');
        $this->Cell($w[1],7,FormatNumber($ToPaidInCapital),'LR',0,'R');
        $this->Cell($w[2],7,FormatNumber($ToSelisihAktiva),'LR',0,'R');
        $this->Cell($w[3],7,FormatNumber($ToRelatedParties),'LR',0,'R');
        $this->Cell($w[4],7,FormatNumber($ToPaidLaba),'LR',0,'R');
        $this->Cell($w[5],7,FormatNumber($ToTotalEquity),'LR',0,'R');
        $this->Ln();

        //Closure line
    }

    function Footer() {
        //Closure line
        $this->Cell(200,0,'','T');
    }

}


?>
